﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class DepartmentInfoBLL:IDepartmentInfoBLL
    {
        private IDepartmentInfoDAL _departmentInfoDAL;
        private UnmanContexts _dbConet;

        public DepartmentInfoBLL(
             UnmanContexts dbConet
            , IDepartmentInfoDAL departmentInfoDAL
            )
        {
            _departmentInfoDAL = departmentInfoDAL;
            _dbConet = dbConet;
        }

        #region 返回父级部门和领导信息下拉列表数据(GetSelectOptions)

        public object GetSelectOptions()
        {
            //先查父级部门数据
            var parentSelect = _dbConet.DepartmentInfo.Where(d => !d.IsDalete)
                                                      .Select(d => new
                                                      {
                                                          value = d.Id,
                                                          title = d.DepartmentName
                                                      })
                                                      .ToList();

            //再查领导信息
            var leaderSelect = _dbConet.StaffInfo.Where(u => !u.IsDalete)
                                                .Select(u => new
                                                {
                                                    value = u.Id,
                                                    title = u.StaffName
                                                })
                                                .ToList();
            //最后返回数据
            var data = new
            {
                parentSelect,
                leaderSelect
            };
            return data;
        }

        #endregion



        #region 新增部门的方法(CreateDepartmentInfo)
        /// <summary>
        /// 新增部门的方法
        /// </summary>
        /// <param name="departmentInfo">新增部门的实体</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CreateDepartmentInfo(DepartmentInfo departmentInfo, out string msg)
        {
            //非空判断
            if (string.IsNullOrWhiteSpace(departmentInfo.DepartmentName))
            {
                msg = "不能为空！";
                return false;
            }
            //查询判断是否重复
            DepartmentInfo oldEntity = _departmentInfoDAL.GetEntities().FirstOrDefault(d => d.DepartmentName == departmentInfo.DepartmentName);
            if (oldEntity != null)
            {
                msg = "部门已存在！";
                return false;
            }
            //给对象补全属性
            departmentInfo.Id = Guid.NewGuid().ToString();
            departmentInfo.CreateTime = DateTime.Now;
            //提交到数据库
            bool isSuccess = _departmentInfoDAL.CreateEntity(departmentInfo);
            //返回结果
            msg = isSuccess ? "添加部门成功" : "添加部门失败";
            return isSuccess;
        }

        #endregion


        #region 获取部门列表数据(GetDepartmentInfos)
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页几条数据</param>
        /// <param name="departmentName">部门名称模糊查询条件</param>
        /// <param name="count">数据总数</param>
        /// <returns></returns>


        public List<GetDepartmentInfoDTO> GetDepartmentInfos(int page, int limit, string departmentName, out int count)
        {
            //linq方法
            var data = from d in _dbConet.DepartmentInfo.Where(d => d.IsDalete == false)
                           //链第一个表查询领导信息
                       join u in _dbConet.StaffInfo.Where(u => u.IsDalete == false)
                       on d.LeaderId equals u.Id
                       into TempDU
                       from uu in TempDU.DefaultIfEmpty()
                           //链第二个表查找父级部门信息
                       join d2 in _dbConet.DepartmentInfo.Where(d2 => d2.IsDalete == false)
                       on d.parentId equals d2.Id
                       into TempDD
                       from dd2 in TempDD.DefaultIfEmpty()
                       select new GetDepartmentInfoDTO
                       {
                           Id = d.Id,
                           CreateTime = d.CreateTime,
                           Description = d.Description,
                           DepartmentName = d.DepartmentName,

                           ParentName = dd2.DepartmentName,
                           LeaderName = uu.StaffName

                       };
            //部门名称不能为空时拼接查询参数
            if (!string.IsNullOrWhiteSpace(departmentName))
            {
                data = data.Where(u => u.DepartmentName.Contains(departmentName));
            }

            //求总数
            count = data.Count();
            //分页
            var listPage = data.OrderByDescending(d => d.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
            //返回结果
            return listPage;


        }
        #endregion


        #region 根据id获取部门详情(GetDepartmentInfoById)
        /// <summary>
        /// 根据id获取部门详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DepartmentInfo GetDepartmentInfoById(string id)
        {
            return _departmentInfoDAL.GetEntityById(id);
        }
        #endregion


        #region 编辑部门的方法(UpdateDepartmentInfo)

        /// <summary>
        /// 编辑部门的方法
        /// </summary>
        /// <param name="departmentInfo">编辑部门的实体</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateDepartmentInfo(DepartmentInfo departmentInfo, out string msg)
        {
            //非空判断
            if (string.IsNullOrWhiteSpace(departmentInfo.Id))
            {
                msg = "id不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(departmentInfo.DepartmentName))
            {
                msg = "部门不能为空！";
                return false;
            }
            //查询旧数据
            DepartmentInfo oldEntity = _departmentInfoDAL.GetEntityById(departmentInfo.Id);
            if (oldEntity == null)
            {
                msg = "部门不存在！";
                return false;
            }
            //判断部门名称是否有修改
            if (oldEntity.DepartmentName != departmentInfo.DepartmentName)
            {
                //查找数据离是否已经有该名字的部门
                var entity = _departmentInfoDAL.GetEntities().FirstOrDefault(d => d.DepartmentName == departmentInfo.DepartmentName);
                if (entity != null)
                {
                    msg = "部门名字重复了！";
                    return false;
                }
            }
            //赋值
            oldEntity.DepartmentName = departmentInfo.DepartmentName;
            oldEntity.Description = departmentInfo.Description;
            oldEntity.parentId = departmentInfo.parentId;
            oldEntity.LeaderId = departmentInfo.LeaderId;
            bool isSuccess = _departmentInfoDAL.UpdateEntity(oldEntity);//更新部门到数据库
            msg = isSuccess ? "更新成功！" : "更新失败！";//返回的消息，三元表达式
            return isSuccess;

        }
        #endregion


        #region 用户软删除的方法(DeleteUserInfo)
        /// <summary>
        /// 用户软删除方法
        /// </summary>
        /// <param name="id">要删除的用户Id</param>
        /// <returns></returns>
        public bool DeleteDepartmentInfo(string id)
        {
            //根据id查用户是否存在
            DepartmentInfo departmentInfo = _departmentInfoDAL.GetEntityById(id);
            if (departmentInfo == null)
            {
                return false;
            }

            //修改用户的状态
            departmentInfo.IsDalete = true;
            departmentInfo.DeleteTime = DateTime.Now;
            //返回结果
            return _departmentInfoDAL.UpdateEntity(departmentInfo);

        }
        #endregion

        #region 部门批量删除
        public bool DeleteDepartmentInfos(List<string> ids)
        {
            foreach (var id in ids)
            {
                //根据用户Id查询用户
                // UserInfo userInfo = userList.FirstOrDefault(u=>u.Id==id);
                DepartmentInfo departmentInfo = _departmentInfoDAL.GetEntityById(id);
                if (departmentInfo == null)
                {
                    continue;//跳出本次循环
                }
                departmentInfo.IsDalete = true;
                departmentInfo.DeleteTime = DateTime.Now;
                //执行修改
                _departmentInfoDAL.UpdateEntity(departmentInfo);
            }
            return true;
        }

        #endregion

    }
}
